//
//  TestZFPlayController.m
//  ProjectDemo
//
//  Created by IOS3 on 2018/11/13.
//  Copyright © 2018 IOS3. All rights reserved.
//
// testMV.mp4



#import "TestZFPlayController.h"
#import <ZFPlayer/ZFPlayer.h>
#import <ZFPlayerControlView.h>
#import <ZFAVPlayerManager.h>



@interface TestZFPlayController ()

@property (nonatomic, strong) ZFPlayerController *player;
@property (nonatomic, strong) UIImageView *containerView;
@property (nonatomic, strong) ZFPlayerControlView *controlView;
@property (nonatomic, strong) UIButton *playBtn;

@end

@implementation TestZFPlayController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initialPlayer];
}

- (void)initialPlayer {

    //
    self.containerView = UIImageView.new;
    [self.containerView setImage:[UIImage imageWithColor:[UIColor randomColor]]];
    
    //
    self.controlView = [ZFPlayerControlView new];
    self.controlView.fastViewAnimated = true;
    
    [self.view addSubview:self.containerView];
    
    ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
    self.player = [ZFPlayerController playerWithPlayerManager:playerManager containerView:self.containerView];
    self.player.controlView = self.controlView;
    self.player.pauseWhenAppResignActive = false;
    
    __weak typeof(self)weakSelf = self;
    self.player.orientationWillChange = ^(ZFPlayerController * _Nonnull player, BOOL isFullScreen) {
        __strong typeof(self)strongSelf = weakSelf;
        [strongSelf setNeedsStatusBarAppearanceUpdate];
    };
    //完成播放
    self.player.playerDidToEnd = ^(id<ZFPlayerMediaPlayback>  _Nonnull asset) {
        // __strong typeof(self)strongSelf = weakSelf;
        DLog(@"播放完按成");
    };
    self.player.playerReadyToPlay = ^(id<ZFPlayerMediaPlayback>  _Nonnull asset, NSURL * _Nonnull assetURL) {
        DLog(@"准备播放");
    };
    
    
    NSURL *url = [NSURL fileURLWithPath:[NSBundle.mainBundle pathForResource:@"testMV" ofType:@"mp4"]];
self.player.assetURL = [NSURL URLWithString:url.absoluteString];
    
    
//    self.player.assetURL = [NSURL URLWithString:@"https://www.apple.com/105/media/us/iphone-x/2017/01df5b43-28e4-4848-bf20-490c34a926a7/films/feature/iphone-x-feature-tpl-cc-us-20170912_1280x720h.mp4"];
    [self.controlView showTitle:@"视频标题" coverURLString:nil fullScreenMode:ZFFullScreenModeLandscape];
}




- (void)addPlayBtn {
    UIButton *btn = UIButton.new;
    [btn setTitle:@"play" forState:UIControlStateNormal];
    btn.frame = CGRectMake(100, 400, 100, 100);
    [btn addTarget:self action:@selector(playBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)playBtnClick:(UIButton *)sender {
    
}



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.player.viewControllerDisappear = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.player.viewControllerDisappear = YES;
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    CGFloat x = 0;
    CGFloat y = CGRectGetMaxY(self.navigationController.navigationBar.frame);
    CGFloat w = CGRectGetWidth(self.view.frame);
    CGFloat h = w*9/16;
    self.containerView.frame = CGRectMake(x, y, w, h);
    
    w = 44;
    h = w;
    x = (CGRectGetWidth(self.containerView.frame)-w)/2;
    y = (CGRectGetHeight(self.containerView.frame)-h)/2;
    self.playBtn.frame = CGRectMake(x, y, w, h);
}


- (UIStatusBarStyle)preferredStatusBarStyle {
    if (self.player.isFullScreen) {
        return UIStatusBarStyleLightContent;
    }
    return UIStatusBarStyleDefault;
}

- (BOOL)prefersStatusBarHidden {
    // return self.player.isStatusBarHidden; iOS8
    return NO;
}

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
    return UIStatusBarAnimationSlide;
}

- (BOOL)shouldAutorotate {
    return self.player.shouldAutorotate;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    if (self.player.isFullScreen) {
        return UIInterfaceOrientationMaskLandscape;
    }
    return UIInterfaceOrientationMaskPortrait;
}



@end
